// pages/cancellation/list/list.js
import Dialog from "../../../miniprogram_npm/@vant/weapp/dialog/dialog";
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabList: ['全部', '未提交', '进行中', '已通过', '已拒绝'],
    searchValue: '', // 搜索值
    nodata: false, //是否没有数据
    listData: [],
    currentPage: 1, // 当前页码
    pageSize: 10, // 每页显示数据
    loading: true,
    remind: '下拉加载更多',
    // activeTab: 0,
    // secondTab1: 0,// 默认为全部list
    secondTab1: 3, // 默认为已通过list

    shijianShow: false, //时间筛选
    bumenShow: false, //部门筛选
    clickdID: '', //选中的部门筛选项
    deptName: '',
    showCalendar: false,
    leftdel: false,
    noleftdel: true, //左滑删除
    add: true, //+号 显示隐藏
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  //时间筛选
  shijianOpen() {
    console.log('时间打开')
    this.setData({
      shijianShow: true,
      add: false
    })
  },
  shijianClose() {
    console.log('时间关闭')
    this.setData({
      shijianShow: false,
      add: true
    })
  },
  //开始时间-结束时间
  onDisplay() {
    this.setData({
      date: this.formatDate(new Date()),
      minDate: new Date(2023, 0, 1).getTime(),
      maxDate: new Date(2023, 12, 31).getTime(),
      showCalendar: true
    });
  },
  onClose() {
    this.setData({
      showCalendar: false
    });
  },
  formatDate(date) {
    date = new Date(date);
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    return `${date.getFullYear()}-${month}-${day}`;
  },
  onConfirm(event) {
    const [start, end] = event.detail;
    this.setData({
      showCalendar: false,
      startDate: this.formatDate(start),
      endDate: this.formatDate(end)
    });
  },
  dropdownChange(e) {
    console.log(e)
  },
  //部门筛选
  bumenOpen() {
    this.setData({
      showDropdownMenu: true,
      add: false
    })
    this.getDeptList()
  },
  bumenClose() {
    this.setData({
      bumenShow: false,
      add: true
    })
  },
  getDeptList() {
    let that = this;
    api.getDeptList({
      success: function (res) {
        console.log(res.data.data, "getDeptList")
        if (res.data.code == 200) {
          that.setData({
            getDeptList: res.data.data,
            // clickdID:res.data.data[0].id
            bumenShow: true
          })
        }
      }
    })
  },
  clickItem(e) {
    console.log(e)
    this.setData({
      deptName: e.currentTarget.dataset.id,
      clickdID: e.currentTarget.dataset.id,
    })
  },
  bumenCenle() {
    this.setData({
      value2: '',
      deptName: '',
      clickdID: '',
      bumenShow: false,
      add: true
    })
    this.clearData()
    console.log(this.data.deptName, '取消---部门筛选')
  },
  bumenOk() {
    this.setData({
      value2: this.data.deptName,
      deptName: this.data.deptName,
      // deptName: '',
      bumenShow: false,
    })
    console.log(this.data.deptName, '确认---部门筛选')
    this.clearData()
  },
  shijiaCenle() {
    console.log(this.data.startDate)
    console.log(this.data.endDate)
    this.setData({
      value1: '',
      startDate: '',
      endDate: '',
      shijianShow: false,
      add: true
    })
    this.clearData()
  },
  shijiaOk() {
    console.log(this.data.startDate, this.data.startDate, '时间筛选 : 开始时间 - 结束时间')
    if (this.data.startDate && this.data.startDate) {
      console.log('不为空')
      this.setData({
        startTimeStr: new Date(this.data.startDate).getTime(),
        endTimeStr: new Date(this.data.endDate).getTime(),
        value1: this.data.startDate + '~' + this.data.endDate,
        shijianShow: false,
        add: true
      })
      this.clearData()
    } else {
      console.log('为空')
      this.setData({
        shijianShow: false
      })
    }
  },
  // 搜索
  onSearch(event) {
    console.log(event)
    this.setData({
      // searchValue: event.detail && event.detail.value ? event.detail.value : event.detail,
      searchValue: event.detail,
    })
    this.clearData()
  },
  secondChange1(event) {
    console.log(event)
    if (event.detail.name == 1) {
      this.setData({
        noleftdel: false,
        leftdel: true
      })
    } else {
      this.setData({
        noleftdel: true,
        leftdel: false
      })
    }
    this.setData({
      secondTab1: event.detail.index,
    })
    this.clearData()
  },
  getData() {
    let that = this;
    api.getDraftList({
      data: {
        searchStr: that.data.searchValue ? that.data.searchValue : '',
        startTimeStr: that.data.startDate ? that.data.startDate : '',
        endTimeStr: that.data.endDate ? that.data.endDate : '',
        deptName: that.data.deptName ? that.data.deptName : '',

        pageNum: that.data.currentPage,
        pageSize: that.data.pageSize,
        tabStr: that.data.secondTab1
      },
      success: function (res) {
        console.log('汇票。。。。。。。。。')
        if (res.data.code == 200) {
          if (res.data.data.list.length > 0) {
            console.log(res.data.data)
            that.data.totalPage = res.data.data.totalPage;
            for (let i = 0; i < res.data.data.list.length; i++) {
              res.data.data.list[i].totalMoney = util.formatFigure(res.data.data.list[i].totalMoney)
            }
            that.setData({
              listData: that.data.listData.concat(res.data.data.list),
              loading: false,
              nodata: false,
              remind: res.data.data.totalPage > res.data.data.pageNum ? '下拉加载更多' : '已经到底啦～',
              totalHp: res.data.data.totalHp,
              totalInvMoney: that.addComma(res.data.data.totalInvMoney)
            })
          } else {
            that.setData({
              nodata: true
            })
          }
        }
      }
    })
  },
  clearData() {
    this.setData({
      listData: [],
      currentPage: 1,
      loading: true,
      nodata: false,
      tips: '暂无数据'
    })
    this.getData()
  },
  //点击新建
  clickadd() {
    wx.navigateTo({
      url: '/pages/addNewdraft/addNewdraft',
    })
  },
  //删除list
  delList(e) {
    console.log(e)
    let index = e.currentTarget.dataset.index;
    let that = this
    Dialog.confirm({
        message: '确定删除吗？',
      })
      .then(() => {
        api.draftListdel({
          data: {
            id: e.currentTarget.dataset.id
          },
          success: function (res) {
            if (res.data.code == 200) {
              that.data.listData.splice(index, 1)
              that.setData({
                listData: that.data.listData
              })
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              })
            }
          }
        })
      })
      .catch(() => {
        // on cancel
        console.log('取消操作')
      });
  },
  //千分位 转换
  addComma(date) {
    let str = '';
    let num = '';
    let newStr = '';
    date = date + '';
    if (date.indexOf('.') != -1) {
      num = date.split('.')[0]
      str = date.split('.')[1]
    } else {
      num = date
    }
    newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    newStr = str == '' ? newStr : newStr + '.' + str;
    return newStr;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      coId : wx.getStorageSync('userInfo').coId,
      unitId : wx.getStorageSync('userInfo').unitId
    })
    // this.clearData()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.totalPage > this.data.currentPage) {
      ++this.data.currentPage;
      this.setData({
        loading: true
      })
      this.getData()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})